package com.tjshop.android;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.tjshop.android.adapter.allCity_adapter;
import com.tjshop.android.adapter.city_adapter;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CityActivity extends AbsBaseActionBarActivity implements
		Handler.Callback {

	private city_adapter locationCityAdapter;
	private city_adapter visitCityAdapter;
	private city_adapter hotCityAdapter;
	private allCity_adapter allCityAdapter;

	private ArrayList<String> locationCityList = new ArrayList<String>();
	private ArrayList<String> visitCityList = new ArrayList<String>();
	private ArrayList<String> hotCityList = new ArrayList<String>();
	private ArrayList<String> allCityList = new ArrayList<String>();

	private Context mContext;
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_citylist);

		mContext = this;
		handler = new Handler(this);
		initTitleBar();

		getCityList();
	}

	// 初始化标题栏
	void initTitleBar() {
		GridView locationCityGv = (GridView) findViewById(R.id.gridview_locationCity);
		locationCityList.add("合肥");
		locationCityAdapter = new city_adapter(locationCityList,
				LayoutInflater.from(this));
		locationCityGv.setAdapter(locationCityAdapter);// 为ListView绑定适配器

		GridView visitCityGv = (GridView) findViewById(R.id.gridview_visitCity);
		visitCityList.add("南京");
		visitCityList.add("合肥");
		visitCityAdapter = new city_adapter(visitCityList,
				LayoutInflater.from(this));
		visitCityGv.setAdapter(visitCityAdapter);// 为ListView绑定适配器

		GridView hotCityGv = (GridView) findViewById(R.id.gridview_hotCity);
		hotCityAdapter = new city_adapter(hotCityList,
				LayoutInflater.from(this));
		hotCityGv.setAdapter(hotCityAdapter);// 为ListView绑定适配器

		ListView allCityLv = (ListView) findViewById(R.id.listview_CityList);
		allCityAdapter = new allCity_adapter(allCityList,
				LayoutInflater.from(this));
		allCityLv.setAdapter(allCityAdapter);// 为ListView绑定适配器
	}

	void getCityList() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message message = new Message();
				String ret = HttpUtil.sendPost(AppConfig.CityUrl, "");
				if (!ret.equals("")) {
					try {
						JSONObject json = new JSONObject(ret);
						if (json.getInt("code") == 0) {
							JSONObject City = json.getJSONObject("data");
							JSONArray hot_citys = City.getJSONArray("hot_city");
							for (int i = 0; i < hot_citys.length(); i++) {
								JSONObject item = hot_citys.getJSONObject(i);
								hotCityList.add(item.getString("name"));
							}
							JSONArray citys = City.getJSONArray("city");
							if (citys.length() > 0) {
								GlobeVars.CityList.clear();
							}
							for (int i = 0; i < citys.length(); i++) {
								JSONObject item = citys.getJSONObject(i);
								if (item.has("name")) {
									Map<String, String> map = new HashMap<String, String>();
									map.put(item.getString("id"),
											item.getString("name"));
									allCityList.add(item.getString("name"));
									GlobeVars.CityList.add(map);
									if (item.getString("name").equals(
											GlobeVars.CurrentCityName)) {
										GlobeVars.CurrentCityId = item
												.getString("id");
									}
								}

							}
							message.what = 0;
							handler.sendMessage(message);
						} else {
							message.what = 1;
							handler.sendMessage(message);
						}
					} catch (JSONException e) {
						message.what = 1;
						handler.sendMessage(message);
						e.printStackTrace();
					}
				}

			}
		}).start();
	}

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 0:
			Toast.makeText(mContext, "获取数据成功！", Toast.LENGTH_SHORT).show();
			hotCityAdapter.notifyDataSetChanged();
			allCityAdapter.notifyDataSetChanged();
			break;
		case 1:
			Toast.makeText(mContext, "获取数据失败!", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		return false;
	}

}
